using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DealerWorld.Forms
{
    public partial class search_vehicle : Form
    {
        public search_vehicle()
        {
            InitializeComponent();

            Main.allVehicles.ForEach(delegate(Vehicle v) { add_lsv_row(v); });
        }

        private void add_lsv_row(Vehicle v)
        {
            string[] row_info = {
                v.customer.name
                ,v.customer.mainPhone
                ,v.licensePlate
                ,v.vin
            };
            ListViewItem lsv_item = new ListViewItem(row_info);
            lsv_item.Tag = v;
            lsv_vehicles.Items.Add(lsv_item);
        }

		private void btnAddToRO_Click(object sender, EventArgs e)
		{
			repair_order.vehForRO.vin = lsv_vehicles.SelectedItems[0].SubItems[3].Text;
			repair_order.vehForRO.licensePlate = lsv_vehicles.SelectedItems[0].SubItems[2].Text;

			this.Close();
		}

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool matchSearch(Vehicle v)
        {
            if (txb_cust_name.Text != "")
                return v.customer.name.ToLower().Contains(txb_cust_name.Text.ToLower());
            if (txb_cust_phone.Text != "")
                return v.customer.mainPhone.Contains(txb_cust_phone.Text);
            if (txb_vin.Text != "")
                return v.vin.ToLower().EndsWith(txb_vin.Text.ToLower());
            if (txb_license.Text != "")
                return v.licensePlate.ToLower().Contains(txb_license.Text.ToLower());
            else return false;
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            lsv_vehicles.Items.Clear();

            List<Vehicle> matchVehicle = Main.allVehicles.FindAll(matchSearch);

            matchVehicle.ForEach(delegate(Vehicle v) { add_lsv_row(v); });
        }

        private void txb_license_KeyUp(object sender, KeyEventArgs e)
        {
            key_pressed(sender, e);
        }

        private void txb_vin_KeyUp(object sender, KeyEventArgs e)
        {
            key_pressed(sender, e);
        }

        private void txb_cust_name_KeyUp(object sender, KeyEventArgs e)
        {
            key_pressed(sender, e);
        }

        private void txb_cust_phone_KeyUp(object sender, KeyEventArgs e)
        {
            key_pressed(sender, e);
        }

        private void search_vehicle_KeyUp(object sender, KeyEventArgs e)
        {
            key_pressed(sender, e);
        }

        private void key_pressed(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btn_search_Click(sender, e);
            }
        }

        private void lsv_vehicles_DoubleClick(object sender, EventArgs e)
        {
            if (lsv_vehicles.SelectedItems.Count == 1)
            {
                Vehicle v = (Vehicle)lsv_vehicles.Items[lsv_vehicles.SelectedIndices[0]].Tag;
                
                new Forms.vehicle(v).ShowDialog();
            }
        }

        private void lsv_vehicles_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                lsv_vehicles_DoubleClick(sender, e);
            }
        }

        private void btnNewVehicle_Click(object sender, EventArgs e)
        {
            Vehicle v = new Vehicle(null);
            //v.customer.name = txb_cust_name.Text;
            //v.customer.mainPhone = txb_cust_phone.Text;
            //v.vin = txb_vin.Text;
            //v.licensePlate = txb_license.Text;
            new Forms.vehicle(v).Show();
        }
    }
}